package com.yxe.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yxe.api.CommonResult;
import com.yxe.pojo.PersonalCertificate;
import com.yxe.service.PersonalCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 身份认证
 * @author 大白羊
 */
@RestController
@RequestMapping("person")
@Api(tags = "身份认证模块")
public class PersonalCertificateController {

    @Autowired
    private PersonalCertificateService personService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("addPerson")
    @ApiOperation("用户信息认证")
    public CommonResult addPerson(@ApiParam("添加用户认证信息") @RequestBody
                                              PersonalCertificate person){
        if (person.name != null && (person.identificationCard.length() == 18)){
            personService.addPersonalCertificate(person);

//            String jsonString = JSONObject.toJSONString(person);
//
//            /**
//             * 存入redis中一份
//             */
//            redisTemplate.opsForHash()
//                         .put(RedisPrefixBy.PersonalCertificateKey.PERSONAL_CERTIFICATE,
//                                 person.identificationCard,
//                                 jsonString);

            return CommonResult.success("添加成功");
        }else {


            return CommonResult.failed("添加失败");
        }
    }

    @GetMapping("findPersonalCertificate")
    @ApiOperation("查看实名认证信息")
    public CommonResult findPersonalCertificate(){

//        identificationCard = "311526199904255719";
//
//        Map<Object, Object> map = redisTemplate.opsForHash()
//                .entries(RedisPrefixBy.PersonalCertificateKey.PERSONAL_CERTIFICATE);
//
//        map.get(id)

        int id = StpUtil.getLoginIdAsInt();
        if (id > 0 ){
            PersonalCertificate personalCertificate = personService.findPersonalCertificate(id);

             return CommonResult.success(personalCertificate);
        }else {
            return CommonResult.failed("查询失败");
        }

    }
}
